package com.gameShare.controller.base

import com.gameShare.entity.dto.user.UserBaseInfoDto
import com.gameShare.enums.misc.ResponseCodeEnum
import com.gameShare.exceptions.BusinessException
import com.gameShare.service.misc.RedisService
import com.gameShare.utils.StringUtils
import jakarta.servlet.http.HttpServletRequest
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RestController

@RestController
open class LoginVerifyController : BaseController(){

    @Autowired
    private lateinit var redisService : RedisService

    /**
     * 获取用户信息通过token如果不存在则抛异常
     *
     * @param httpServletRequest 用户请求
     * @return
     */
    fun getUserInfoByToken(httpServletRequest: HttpServletRequest): UserBaseInfoDto {
        val token = httpServletRequest.getHeader("token")
            ?: throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR)
        val userInfo = redisService.getUserTokenInfo(token)
            ?: throw BusinessException(ResponseCodeEnum.LOGIN_ERROR)
        return userInfo
    }

    /**
     * 获取用户id
     *
     * @param httpServletRequest 用户请求
     * @return 用户id
     */
    fun getUserIdByToken(httpServletRequest: HttpServletRequest) : String{
        return getUserInfoByToken(httpServletRequest).id
    }
}